package cn.yeziji.netty.adapter;

import cn.yeziji.forum.basic.netty.Message;
import cn.yeziji.forum.basic.netty.user.UserMessage;
import cn.yeziji.forum.common.key.MessageKey;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * 心跳检测处理器
 *
 * @author gzkemays
 * @since 2022/11/09 3:01 PM
 */
@Slf4j
public class HeartAdapter extends SelectedTypes<UserMessage> implements CommunicationAdapters {
  RabbitTemplate rabbitTemplate = getRabbitTemplate();

  public HeartAdapter(Integer type, String text) {
    super(type, text, UserMessage.class);
  }

  @Override
  public void notice(ChannelHandlerContext channelHandlerContext) {
    Message message = new Message();
    message.setType(MessageKey.HEART_FLAG);
    message.setContext(String.valueOf(getNettyWebsocketHandler().getConnectChannels().size()));
    writeAndFlush(channelHandlerContext, message);
    log.info("客户端发送心跳，刷新统计 --> {}", message);
  }

  @Override
  public Object dispose(ChannelHandlerContext channelHandlerContext) {
    return null;
  }
}
